﻿using System;
using EntityFrameworkContrib.Design.VisualStudio.Properties;
using EnvDTE;

namespace EntityFrameworkContrib.Design.VisualStudio
{
	static class LoggingHelper
	{
		internal static void LogOutputWindowPaneMessage(Project _project, string message)
		{
			if (message == null)
			{
				throw new ArgumentNullException("message");
			}
			OutputWindowPane outputWindowPane = GetOutputWindowPane(_project);
			if (outputWindowPane != null)
			{
				outputWindowPane.OutputString(message + Environment.NewLine);
			}
		}

		static OutputWindowPane GetOutputWindowPane(Project _project)
		{
			OutputWindowPane pane = null;
			if (_project.DTE != null)
			{
				Window window = _project.DTE.Windows.Item("{34E76E81-EE4A-11D0-AE2E-00A0C90FFFC3}");
				if (window == null)
				{
					return pane;
				}
				window.Activate();
				window.Visible = true;
				OutputWindow window2 = window.Object as OutputWindow;
				if (window2 == null)
				{
					return pane;
				}
				try
				{
					pane = window2.OutputWindowPanes.Item( Resources.EDMOutputWindowPaneTitle );
				}
				catch (ArgumentException)
				{
					pane = window2.OutputWindowPanes.Add( Resources.EDMOutputWindowPaneTitle );
				}
				catch (NullReferenceException)
				{
					pane = window2.OutputWindowPanes.Add( Resources.EDMOutputWindowPaneTitle );
				}
				if (pane != null)
				{
					pane.Activate();
				}
			}
			return pane;
		}
	}
}